package com.changgou.business.listener;

import com.changgou.business.config.RabbitMQConfig;
import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AdListener {

    //从mq中取消息,指定队列
    @RabbitListener(queues = RabbitMQConfig.AD_UPDATE_QUEUE)
    public void getMessage(String message){
        System.out.println("接受到的消息是:" + message);


        //远程请求lua执行更新
        //第一步,实例化httpClient
        OkHttpClient okHttpClient = new OkHttpClient();
        //第四步,设置请求lua地址
        String url = "http://192.168.200.128/ad_update?position="+message;
        //第三步,设置请求对象
        Request request = new Request.Builder().url(url).build();
        //第二步,执行请求
        Call call = okHttpClient.newCall(request);
        //第五步,返回响应
        call.enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求成功:" + response.message());
            }
        });

    }
}
